package com.yanqu.road.server.timer.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarState;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.entity.enums.eUnionWarWinMark;
import com.yanqu.road.entity.log.cross.LogUnionWarActivityResult;
import com.yanqu.road.entity.unionwar.GuessUnionInfo;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;
import com.yanqu.road.server.manager.unionwar.*;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AddUnionWarChampionTask extends GeneralTask {
    private static final Logger logger = LogManager.getLogger(AddUnionWarChampionTask.class.getName());
    private boolean isRun = false;
    private List<Integer> activityIdList = new ArrayList<>();
    @Override
    public void doRun() {
        if (!isRun) {
            isRun = true;
            Map<Integer, ActivityInfo> activityInfoMap = CrossUnionWarActivityMgr.getOpenActivityMap();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                if (!activityIdList.contains(activityInfo.getActivityId())) {
                    CurrentTimeInfo currentTimeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId());
                    //战斗阶段已结束
                    if (DateHelper.getCurrentSecond() >= activityInfo.getEndTime() || currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.ReceiveTime) {
                        logger.info("start addChampion {}", activityInfo.getActivityId());
                        if(CrossUnionWarRankMgr.addChampion(activityInfo)){
                            activityIdList.add(activityInfo.getActivityId());

                            addResultLog(activityInfo.getActivityId());
                        }
                    }
                }
            }
            isRun = false;
        }
    }

    /**
     * 冠亚军/商会
     * 竞猜热度积分赛前八/决赛前二
     */
    private void addResultLog(int activityId) {
        try {
            int allSession = CrossUnionWarActivityMgr.getAllSession(activityId, 2);
            if(allSession == 0){
                return;
            }
            List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityId, CrossBaseServer.getInstance().getServerId());
            for (int groupId : groupIdList) {

                CrossUnionWarState state = CrossUnionWarMgr.getCrossUnionWarState(activityId, 2, allSession, groupId);

                List<LogUnionWarActivityResult> logList = new ArrayList<>();

                //冠亚军
                if(state != null && state.getState() == 2) {
                    List<CrossUnionWarDetail> detailList = CrossUnionWarMgr.getCrossUnionWarDetailList(activityId, 2, allSession, groupId);
                    if (detailList != null) {
                        for (CrossUnionWarDetail detail : detailList) {
                            if(detail.getWinMark() == eUnionWarWinMark.Win.getValue()){
                                logList.add(new LogUnionWarActivityResult(activityId, 2, detail.getUnionUid(), 1, 1, groupId));
                            }else if(detail.getWinMark() == eUnionWarWinMark.Lose.getValue()){
                                logList.add(new LogUnionWarActivityResult(activityId, 2, detail.getUnionUid(), 1, 2, groupId));
                            }
                        }
                    }
                }

                //积分赛竞猜热度
                addGuessHot(activityId, 1, 8, logList, groupId);
                //决赛竞猜热度
                addGuessHot(activityId, 2, 2, logList, groupId);
                for (LogUnionWarActivityResult result : logList) {
                    CrossAutoLogMgr.add(result);
                }
            }
        }catch (Exception e){
            getLOGGER().error(e.getMessage());
        }
    }

    private void addGuessHot(int activityId, int stage, int n, List<LogUnionWarActivityResult> logList, int groupId) {
        CrossUnionWarGuessDataListModel guessModel = CrossUnionWarGuessMgr.getCrossUnionWarGuessDataListModel(activityId, stage, groupId);
        if(guessModel != null){
            Map<String, GuessUnionInfo> guessUnionInfoMap = guessModel.getGuessUnionInfoMap();
            List<GuessUnionInfo> guessUnionInfoList = new ArrayList<>();
            for (Map.Entry<String, GuessUnionInfo> infoEntry : guessUnionInfoMap.entrySet()) {
                guessUnionInfoList.add(infoEntry.getValue());
            }
            guessUnionInfoList.sort((o1, o2) -> o2.getGuessPopularity() - o1.getGuessPopularity());
            int size = Math.min(n, guessUnionInfoList.size());
            for (int i = 0; i < size; i++) {
                GuessUnionInfo info = guessUnionInfoList.get(i);
                logList.add(new LogUnionWarActivityResult(activityId, stage, info.getUnionUid(), 2, i + 1, groupId));
            }
        }
    }
}
